<?php 
$this->headLink(array('href' => '/cms/styles/style.css', 'rel' => "stylesheet", 'type' => "text/css"));
$this->headLink(array('href' => '/cms/styles/data-table.css', 'rel' => "stylesheet", 'type' => "text/css"));

$this->headScript()
	->appendFile('/cms/scripts/dojo/dojo.js','',array('djConfig'=> 'parseOnLoad:true, isDebug:false'))
	->setAllowArbitraryAttributes(true);

$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/shadowbox/build/adapter/shadowbox-base.js');
$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/shadowbox/build/shadowbox.js');

$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/content.js');
//$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/Popup.js');
$this->headScript(Zend_View_Helper_HeadScript::FILE,'/cms/scripts/TableData.js');
$this->headScript(Zend_View_Helper_HeadScript::SCRIPT,"
	var newsId = {$this->news_id};
	//filter_setup = ['from_date','".date('Y-m-d',mktime(0,0,0,date('m'),date('d')-10,date('Y')))."','to_date','".date('Y-m-d')."'];");
$this->headScript(Zend_View_Helper_HeadScript::SCRIPT,<<<EOD

//dojo.require("dijit.form.DateTextBox");
dojo.require("dijit.InlineEditBox");
dojo.require("dojo.parser");
  
var shadowbox_callback = function () {};

reset_shadowbox_callback = function () {
	shadowbox_callback = function () {};
} 

Shadowbox.loadSkin('classic', '/cms/scripts/shadowbox/src/skin');
Shadowbox.loadLanguage('ru', '/cms/scripts/shadowbox/build/lang');
Shadowbox.loadPlayer(['html', 'iframe', 'img', 'swf'], '/cms/scripts/shadowbox/build/player');

window.onload = function () {
	Shadowbox.init(
    	{
			modal:		true,
			onClose: function() { shadowbox_callback() }
		}
	);
	
	newsTable = new tableData('mainNewsTable');
	
	newsTable.registerWidget('news_public',
		function (params) {
			var divEl = document.createElement('DIV');
			divEl.className = 'icon';
			
			var imgEl = document.createElement('IMG');
			imgEl.src = '/cms/images/' + (params['value']=='0' ? 'eye_disabled.png' : 'eye.png');
			
			var aEl = document.createElement('A');
			aEl.href = 'javascript:;';
			aEl.onclick = function () {
					dojo.xhrGet({
						url: "/cms/news/publish/id/"+params['id'],
						load: function(response, ioArgs) {
						  imgEl.src = '/cms/images/' + (parseInt(response) == 1 ? 'eye.png' : 'eye_disabled.png'); 
						  return response;
						},
						error: function(response, ioArgs){
						  alert("An error occurred, with response: " + response);
						  return response;
						},
						handleAs: "text",
						sync: true
					  });
				}
			
			
			divEl.appendChild(aEl);
			aEl.appendChild(imgEl);
			
			return divEl;
		});
		
	newsTable.registerWidget('news_date',
		function (params) {
			var divEl = document.createElement('DIV');
			divEl.innerHTML = params['value'];
			divEl.setAttribute('dojoType','dijit.form.DateTextBox(');
			
			new dijit.InlineEditBox({
									autoSave:true,
									renderAsHtml:true,
									onChange: function (value) {
											dojo.xhrGet({
											    url: "/cms/news/change/id/"+params['id']+"/add_date/"+value,
											    load: function(response, ioArgs){
											      return response;
											    },
											    error: function(response, ioArgs){
											      alert(response);
											      return response;
											    },
											    handleAs: "text"
											  });
									
										}
								}, divEl);
			
			return divEl;
		});
		
	newsTable.registerWidget('news_title',
		function (params) {
			var divEl = document.createElement('div');
			divEl.setAttribute('dojoType','dijit.InlineEditBox');
			divEl.innerHTML = params['value']=='' ? 'Нет заголовка' : params['value'];
			
			new dijit.InlineEditBox({
									autoSave:true,
									renderAsHtml:true,
									onChange: function (value) {
											var temp_form = dojo.create('form');
											dojo.create('input',{'name':'title','value':value},temp_form);
											
											dojo.xhrPost({
											    url: "/cms/news/change/id/"+params['id'],
											    form: temp_form,
											    load: function(response, ioArgs){
											      return response;
											    },
											    error: function(response, ioArgs){
											      alert(response);
											      return response;
											    },
											    handleAs: "text"
											  });
									
										}
								}, divEl);
			
			
								
			
			return divEl;
		});
		
	newsTable.registerWidget('news_delete',
		function (params) {
			var divEl = document.createElement('DIV');
			divEl.className = 'icon';
			
			var imgEl = document.createElement('IMG');
			imgEl.src = '/cms/images/cross.png';
			
			var aEl = document.createElement('A');
			aEl.href = 'javascript:;';
			aEl.onclick = function () {
					var delete_params = new Array();
					for (var f=0;f<filter_setup.length;f++) {
						delete_params.push(filter_setup[f]);
					}
					delete_params.push('act','del','note_id',params['id']);
					callData(delete_params);
				}
			
			
			divEl.appendChild(aEl);
			aEl.appendChild(imgEl);
			
			return divEl;
		});
		
	callData([]);
}


var addNewsNote = function() {
	current_page = 1;
	limit_setup = [0,rows_per_page];
	callData(['act','add']);
}
	  
var callData = function (params) {
	dojo.xhrGet({
	    url: "/cms/news/widget/id/"+newsId+"/limit_from/"+limit_setup[0]+"/limit_amount/"+limit_setup[1]+'/'+params.join('/'),
	    load: function(responseObject, ioArgs){
	      var pagesPlace = document.getElementById('pagesPlace');
	      pagesPlace.innerHTML = '';
	      
	      var total_pages = Math.ceil(responseObject['total_length']/rows_per_page);
	      
	      var appendPageAction = function (page) {
	        var aEl = document.createElement('A');
	      	aEl.innerHTML = i;
	      	aEl.href = 'javascript:;';
	      	aEl.onclick = function () { gotoPage(page) }
	      	return aEl;
	      }
	      
	      var appendDelimetr = function () {
	        var delimetr = document.createElement('SPAN');
	      	delimetr.innerHTML = '&nbsp;';
	      	return delimetr;
	      }
	      
	      
	      if (current_page>1) {
	      	var aEl = document.createElement('A');
	      	aEl.innerHTML = '&lt; Предыдущая';
	      	aEl.href = 'javascript:;';
	      	aEl.onclick = function () { gotoPage(current_page-1) }
	      	pagesPlace.appendChild(aEl);
	      	pagesPlace.appendChild(appendDelimetr());
	      }
	      
	      var start_from = current_page-3;
	      if (start_from>1) {
	      	var dots = document.createElement('SPAN');
	      	dots.innerHTML = '...&nbsp;';
	      	pagesPlace.appendChild(dots);
	      } else {
	      	start_from = 1;
	      }
	      for (var i=start_from;i<current_page;i++) {
	      	pagesPlace.appendChild(appendPageAction(i));
	      	
	      	pagesPlace.appendChild(appendDelimetr());
	      }
	      
	      var cur_pg = document.createElement('SPAN');
	      cur_pg.innerHTML = current_page;
	      pagesPlace.appendChild(cur_pg)
	      
	      pagesPlace.appendChild(appendDelimetr());
	      	
	      	
	      var end_to = current_page+3;
	      if (end_to>total_pages) end_to = total_pages;
	      
	      for (var i=current_page+1;i<=end_to;i++) {
	      	pagesPlace.appendChild(appendPageAction(i));
	      	
	      	pagesPlace.appendChild(appendDelimetr());
	      }
	      
	      if (end_to<total_pages) {
	        var dots = document.createElement('SPAN');
	      	dots.innerHTML = '...&nbsp;';
	      	pagesPlace.appendChild(dots);
	      }
	      
	      if (current_page<total_pages) {
	      	var aEl = document.createElement('A');
	      	aEl.innerHTML = 'Следующая &gt;';
	      	aEl.href = 'javascript:;';
	      	aEl.onclick = function () { gotoPage(current_page+1) }
	      	pagesPlace.appendChild(aEl);
	      	pagesPlace.appendChild(appendDelimetr());
	      }
	      
		  newsTable.draw(responseObject['data']);
		  
		  
		  //dojo.parser.parse(document.getElementById('mainNewsTable'));
		  
	      return responseObject;
	    },
	    error: function(response, ioArgs){
	      alert("An error occurred, with response: " + response);
	      return response;
	    },
	    handleAs: "json",
	    sync: true
	  });
}

var current_page = 1;
var rows_per_page = 10;
var limit_setup = [0,rows_per_page];

var filter_setup = new Array();
var MakeFilter = function () {
	filter_setup = ['from_date',document.getElementById('from_date').value,'to_date',document.getElementById('to_date').value];
	if (document.getElementById('publish_f').checked) {
		filter_setup.push('publish_f','1');
	}
	
	gotoPage(1);
}

var gotoPage = function (page) {
	current_page = page;
	limit_setup = [(page-1)*rows_per_page,rows_per_page];
	callData(filter_setup);
}
EOD
);


$this->layout()->body_attr = 'class="modal"';
?>

<p class="stnd_font">
  С: <!-- a href="#">2009-01-30</a --><input type="text" dojoType="dijit.form.DateTextBox" name="from_date" id="from_date" value="<?= date('Y-m-d',mktime(0,0,0,date('m'),date('d')-10,date('Y'))) ?>" />
  По: <!--  a href="#">2009-01-30</a --><input type="text" dojoType="dijit.form.DateTextBox" name="to_date" id="to_date" value="<?= date('Y-m-d') ?>" />
  &nbsp;
  <input type="checkbox" name="publish_f" id="publish_f" value="1" />
  <label for="publish_f">Только опубликованные</label>
  &nbsp;
  <input type="button" style="width:130px" value="Фильтровать" class="okButton" onclick="MakeFilter()" /><br />
  <button style="width:200px" class="okButton" onclick="addNewsNote()"><img src="/cms/images/plus.png" width="16" height="16" align="absmiddle" /> Добавить новость</button>
</p>
<p class="stnd_font">Страницы: <span id="pagesPlace">&nbsp;</span></p>
<table width="100%" cellspacing="0" class="table-data">
<thead>
  <tr>
    <th width="37" nowrap="nowrap" scope="col">&nbsp;</th>
    <th width="160" nowrap="nowrap" scope="col">Дата</th>
    <th nowrap="nowrap" scope="col">Заголовок</th>
<?php for ($m=0;$m<count($this->data_map);$m++) {?>
    <th width="100" nowrap="nowrap" scope="col"><?= $this->data_map[$m] ?></th>
<?php } ?>
    <th width="34" nowrap="nowrap" scope="col">&nbsp;</th>
  </tr>
</thead>
<tbody id="mainNewsTable"></tbody>
</table>